import Taro from '@tarojs/taro';
import { Query, Store, StoreConfig } from "@datorama/akita";
import { produce } from 'immer';
import { AccessItem, Auth, JsCode, Org } from "@/models/Auth";
import { APPROVE_STATUS } from '@/constants/user';

function createInitialState(): Auth {
  return {
    jsCodeObj: { code: "", expires: new Date().getTime() },
    loginStatus: false,
    token: "",
    refresh_token: "",
    expires_in: new Date().getTime(),
    userId: "",
    roles: [],
    realName: "",
    orgs: [],
    subscription: [],
    userInfo: {
      nickName: "",
      avatarUrl: "",
      gender: 0,
      language: "zh_CN",
      country: "",
      province: "",
      city: "", 
    },  
    userApproveStatus: APPROVE_STATUS.NEVER,
    orgApproveStatus: APPROVE_STATUS.NEVER,
    idCardNo: "",
    sexual: 0,
    desc: "",
  }
}

@StoreConfig({
  name: "auth",
  producerFn: produce,
  resettable: true,
  //缓存 300秒(5分钟)
  // cache: {
  //   ttl: 300000
  // }
})
export class AuthStore extends Store<Auth> {
  private static _instance: AuthStore;

  constructor() {
    super(createInitialState())
  }

  //单例方法
  public static getInstance(): AuthStore {
    if (this._instance == null) {
      this._instance = new AuthStore();
    }
    return this._instance;
  }
}

export class AuthQuery extends Query<Auth> {
  jsCode$ = this.select("jsCodeObj");
  loginStatus$ = this.select("loginStatus");
  expires_in$ = this.select("expires_in");
  userId$ = this.select("userId");
  roles$ = this.select("roles");
  realName$ = this.select("realName");
  orgs$ = this.select("orgs");
  token$ = this.select("token");
  refreshToken$ = this.select("refresh_token");
  userInfo$ = this.select("userInfo"); 
  userApproveStatus$ = this.select("userApproveStatus");
  orgApproveStatus$ = this.select("orgApproveStatus");
  idCardNo$ = this.select('idCardNo');
  subscription$ = this.select('subscription');
  sexual$ = this.select('sexual');
  desc$ = this.select('desc')

  get jsCode(): JsCode {
    return this.getValue().jsCodeObj;
  }
  get realName(): string {
    return this.getValue().realName;
  }
  get orgs(): Org[] {
    return this.getValue().orgs;
  }
  get loginStatus(): boolean {
    return this.getValue().loginStatus;
  }
  get expires_in(): number {
    return this.getValue().expires_in;
  }
  get userId(): string {
    return this.getValue().userId;
  }
  get roles(): string[] {
    return this.getValue().roles;
  }
  get token(): string {
    return this.getValue().token;
  }
  get refreshToken(): string {
    return this.getValue().refresh_token;
  }
  get userInfo(): Taro.UserInfo {
    return this.getValue().userInfo;
  }
  get userApproveStatus(): number {
    return this.getValue().userApproveStatus
  }
  get orgApproveStatus(): number {
    return this.getValue().orgApproveStatus
  }
  get idCardNo(): string {
    return this.getValue().idCardNo
  }
  get subscription(): AccessItem[] {
    return this.getValue().subscription;
  }
  get sexual(): number {
    return this.getValue().sexual;
  }
  get desc(): string {
    return this.getValue().desc;
  }


  constructor(protected store: AuthStore) {
    super(store);
  }
}

export const makeStore = (): [AuthStore, AuthQuery] => {
  //单例模式
  const store = AuthStore.getInstance();
  const query = new AuthQuery(store);

  return [store, query];
};
